package org.go.smart;

import java.util.HashMap;
import java.util.Map;

import org.go.smart.log.GSLogger;
import org.go.smart.statistics.HeaterStatistics;
import org.go.smart.temperature.TemperatureChecker;
import org.go.smart.temperature.WeeklyScheduler;

public class HouseController {
	
	public  static final int NOT_INITIALISED_VALUE = -100;

	private Map<String, RoomController> roomControllers = new HashMap<String, RoomController>();
	private boolean heaterActive = false;
	private HeaterStatistics heaterStatistics;
	private TemperatureChecker temperatureChecker;
	
	private static HouseController instance;
	
	private HouseController() {
		this.roomControllers.put("bedroom1", new RoomController("bedroom1"));
		this.roomControllers.put("bedroom2", new RoomController("bedroom2"));
		this.roomControllers.put("livingroom", new RoomController("livingroom"));
		this.roomControllers.put("lobby", new RoomController("lobby"));
		this.roomControllers.put("bathroom", new RoomController("bathroom"));
		
		heaterStatistics = new HeaterStatistics();
		temperatureChecker = new TemperatureChecker();
		new Thread(temperatureChecker).start();
	}
	
	public static HouseController getInstance() {
		if (instance == null) {
			instance = new HouseController();
		}
		return instance;
	}
	
	public Map<String, String> getReadTemperatures() {
		Map<String, String> temperatures = new HashMap<String, String>();
		for (String roomName : this.getRoomControllers().keySet()) {
			RoomController roomController = this.getRoomControllers().get(roomName);
			temperatures.put(roomName, roomController.getReadTemperature());
		}
		
		return temperatures;
	}
	
	public Map<String, String> getWantedTemperatures() {
		Map<String, String> temperatures = new HashMap<String, String>();
		for (String roomName : this.getRoomControllers().keySet()) {
			RoomController roomController = this.getRoomControllers().get(roomName);
			temperatures.put(roomName, roomController.getWantedTemperature());
		}
		
		return temperatures;
	}
	
	public RoomController getRoomController(final String room) {
		return this.getRoomControllers().get(room);
	}

	public boolean isHeaterActive() {
		return heaterActive;
	}

	public Map<String, RoomController> getRoomControllers() {
		return roomControllers;
	}
	
	public void stopHeater() {
		//TODO: actually stop the heater
		
		this.heaterActive = false;
		this.heaterStatistics.endLastEntry();
		GSLogger.getInstance().info("Heater stopped!");
	}
	
	public void startHeater() {
		//TODO: actually stop the heater
		
		this.heaterActive = false;
		this.heaterStatistics.addEntry();
		GSLogger.getInstance().info("Heater started!");
	}

	
	
	
}
